package com.jyx.conversition.pdf;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/*import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;*/

public class TPdfTest {

	
	public static void main(String[] args) {
		// 1、建立pdf文档对象
				Document doc = new Document();
				try {
					// 2、建立个书写器与doc文档对象那个进行关联，并写入磁盘
					PdfWriter.getInstance(doc, new FileOutputStream("D:/toPdf.pdf"));
					// 3.设定属性
					doc.addTitle("标题！");
					doc.addAuthor("Tony");
					
					// 创建字体
					BaseFont bfChinese = null;
					Font fontChinese = null;
					
					try {
						bfChinese=BaseFont.createFont("Helvetica","Cp1252",BaseFont.NOT_EMBEDDED);
						fontChinese = new Font(bfChinese,14,Font.NORMAL);
					} catch (IOException e) {
						e.printStackTrace();
					}

					// 5.打开文档
					doc.open();
					doc.add(new Paragraph("亲爱的同学们，欢迎大家成为潭州人！",fontChinese));
					
					// 6、关闭文档
					doc.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (DocumentException e) {
					e.printStackTrace();
				}
	}
	
	
	/**
	 * 生成pdf文件
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void generatePDF(HttpServletRequest request, HttpServletResponse response)
			throws IOException{
		
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		
		//1、建立doc对象
		Document doc = new Document();
		try {
			PdfWriter.getInstance(doc, buffer);
			
			// 2、设定文档相关属性
			doc.addTitle("TO PDF ！");
			doc.addAuthor("Tony");
			
			// 3、设定表格对象
			Table table = new Table(3); // 3列的table
			
			// 4、设定表格属性
			float[] widths = {0.30f,0.60f,0.40f}; // 每列表格的宽度
			table.setWidths(widths); 
			table.setBorderWidth(1); //边框宽度
			table.setBorderColor(new Color(0,0,255)); // 边框颜色
			table.setPadding(0);
			table.setSpacing(0);
			
			
			// 5、中文处理
			// 创建字体
			BaseFont bfChinese = null;
			Font fontChinese = null;
			
			try {
				bfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
				fontChinese = new Font(bfChinese,14,Font.NORMAL);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			// 添加页眉页脚
			HeaderFooter header = new HeaderFooter(new Phrase("JYX",fontChinese),false);
			HeaderFooter footer =  new HeaderFooter(new Phrase("JYX",fontChinese),false);
			
			doc.setHeader(header);
			doc.setFooter(footer);
			
			// 设置表格标题
			Phrase ph = new Phrase("个人信息表",fontChinese);
			Cell cell = new Cell(ph);
			cell.setColspan(3); // 跨列
			cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 居中
			table.addCell(cell);
			
			// 设置表头
			Phrase p1 = new Phrase("姓名",fontChinese);
			Phrase p2 = new Phrase("年龄",fontChinese);
			Phrase p3 = new Phrase("地址",fontChinese);
			
			Cell cell1 = new Cell(p1);
			Cell cell2 = new Cell(p2);
			Cell cell3 = new Cell(p3);
			
			table.addCell(cell1);
			table.addCell(cell2);
			table.addCell(cell3);
			
			// 添加数据
			
			List<User> list = new ArrayList<User>();
			list.add(new User("张飞", 21, "三国"));
			list.add(new User("林冲", 22, "水浒"));
			list.add(new User("悟空", 23, "西游"));
			list.add(new User("宝玉", 24, "红楼"));
			for(User user:list){
				Phrase p1_1 = new Paragraph(user.getName(),fontChinese);
				Phrase p1_2 = new Paragraph(user.getAge()+"",fontChinese);
				Phrase p1_3 = new Paragraph(user.getAddress(),fontChinese);
				
				Cell cell1_1 = new Cell(p1_1);
				Cell cell1_2 = new Cell(p1_2);
				Cell cell1_3 = new Cell(p1_3);
				
				table.addCell(cell1_1);
				table.addCell(cell1_2);
				table.addCell(cell1_3);
			}
			doc.open();
			doc.add(table);
			doc.close();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		response.setContentType("application/pdf");
		response.setHeader("Content-Disposition", "attachment;Filename="+"tanzhou.pdf");
		response.setContentLength(buffer.size());
		
		ServletOutputStream out = response.getOutputStream();  // 得到输出流对象
		buffer.writeTo(out); 
		out.flush();
		out.close();
	}
}


class User{
	
	public User(){}
	public User(String name, Integer age, String address){
		this.name = name;
		this.age = age;
		this.address = address;
	}
	
	private String name;
	private Integer age;
	private String address;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}
